using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using BO;
using Telerik.WinControls.UI;

namespace AnimalFarm.Product_manage
{
    public partial class AddTreatmentProduct : Telerik.WinControls.UI.RadForm
    {
        B_DairyProduct B_dairy = new B_DairyProduct();
        B_MilkTreatment B_milkTreat = new B_MilkTreatment();
        B_Mix B_mix = new B_Mix();
        B_TreatmentType B_treatType = new B_TreatmentType();
        B_Treatment_Product B_treatmentProduct = new B_Treatment_Product();

        public AddTreatmentProduct()
        {
            InitializeComponent();
        }

        private void AddTreatmentProduct_Load(object sender, EventArgs e)
        {
            loadDairyProduct();
            loadMilkTreatment();
        }

        //load dairy product
        private void loadDairyProduct()
        {
            DataTable aTable = B_dairy.GetAllDairyProduct();
            grvDairyProduct.DataSource = aTable;
        }

        //load milktreatment
        private void loadMilkTreatment()
        {
            DataTable aTable = B_milkTreat.GetAllMilkTreatment();
            drdMilkTreatment.DataSource = aTable;
            drdMilkTreatment.DisplayMember = "Description";
            drdMilkTreatment.ValueMember = "MilkTreatmentID";
            drdMilkTreatment.Text = "(Select a milk treatment)";
        }

        private void drdMilkTreatment_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (drdMilkTreatment.SelectedIndex != -1)
                {
                    int treatmentID = int.Parse(drdMilkTreatment.SelectedValue.ToString());
                    MilkTreatment_Info info = B_milkTreat.GetOneMilkTreatment(treatmentID);
                    txtDescription.Text = info.Description;
                    lblTime.Text = info.Time.ToLongDateString();
                    Mix_Info mixinfo = B_mix.GetOneMix(info.MixtID);
                    lblMix.Text = mixinfo.MixName;
                    TreatmentType_Info typeInfo = B_treatType.GetOneTreatmentType(info.TreatmentTypeID);
                    lblTreatmentType.Text = typeInfo.TreatmentTypeName;

                }
            }
            catch (Exception ex) { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAddTreatment_Click(object sender, EventArgs e)
        {
            if (grvDairyProduct.SelectedRows.Count > 0)
            {
                int[] dairyProductIDs = new int[grvDairyProduct.SelectedRows.Count];
                //set a array of dairy product ID
                for (int i = 0; i < grvDairyProduct.SelectedRows.Count; i++)
                {
                    GridViewRowInfo row = grvDairyProduct.SelectedRows[i];
                    dairyProductIDs[i] = int.Parse(row.Cells["DairyProductID"].Value.ToString());
                }

                int milkTreatmentID = int.Parse(drdMilkTreatment.SelectedValue.ToString());
                int quanlity = int.Parse(txtQuanlity.Text);
                //insert data for the treamtment product table
                bool insert = true;
                for (int i = 0; i < dairyProductIDs.Length; i++)
                {
                    Treatment_Product_Info treatment = new Treatment_Product_Info();
                    treatment.DairyProductID = dairyProductIDs[i];
                    treatment.MixTreatmentID = milkTreatmentID;
                    treatment.Quanlity = quanlity;
                    insert = B_treatmentProduct.CreateOneTreatment_Product(treatment);
                }
                if (insert)
                {
                    MessageBox.Show(this, "Adding treatment product is successful", "Treatment product", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    resetFields();
                }
            }
        }

        private void resetFields()
        {
            txtDescription.Text = "";
            txtQuanlity.Text = "";
            txtQuanlity.Focus();
        }
    }
}
